package com.cyys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyys.entity.Collects;
import com.cyys.mapper.CollectsMapper;
import com.cyys.response.Result;
import com.cyys.service.CollectsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Wrapper;
import java.util.List;

/**
 * @author Administrator
 * @description CollectsController
 * @date 2022-06-10 11:46
 */
@RestController
@RequestMapping("/Collects")
public class CollectsController {

    @Autowired
    private CollectsService collectsService;

    @Autowired
    private CollectsMapper collectsMapper;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<Collects> list = collectsService.list();
        return Result.ok().data("list", list);
    }

    /**
     * 多表查询用户id
     *
     * @param id
     * @return
     */
    @GetMapping("/Collects_goods")
    public Result Collects(Integer id) {

        Collects c = new Collects();
        c.setId(id);
        List<Collects> list = collectsService.CollectsFind(c);
        return Result.ok().data("list", list);
    }

    /**
     * 多表查询openid
     *
     * @param id
     * @return
     */

    @ApiOperation(value = "查询name id")
    @GetMapping("/CollectsGoods")
    public Result CollectsGoods(@RequestParam( value = "id") Integer id,@RequestParam( value = "name") String nane) {
        System.out.println(nane);
        Collects c = new Collects();
        c.setId(id);
        c.setName(nane);
        List<Collects> list = collectsService.CollectsFinds(c);
        System.out.println(list);
        if (list != null && !list.isEmpty()){

            return Result.ok().data("list", list);
        }

        return Result.error();
    }

    /**
     * 删除
     */
    @GetMapping("/Collects_delete")
    public  Result Collects_delete(String name){
        QueryWrapper<Collects> wrapper=new QueryWrapper();
        wrapper.eq("name",name);
        int Collects_delete = collectsMapper.delete(wrapper);
        if (Collects_delete == 0) {
            return Result.error();
        }
        return Result.ok();
    }


    @ApiOperation(value = "添加收藏")
    @GetMapping("/add")
    public Result add(String userid, String name, String description,
                      String mainImg, BigDecimal price, Integer sum, String status, Integer storeId){
        Collects collects=new Collects();
        collects.setUserid(userid);
        collects.setName(name);
        collects.setDescription(description);
        collects.setMainimg(mainImg);
        collects.setPrice(price);
        collects.setSum(sum);
        collects.setStatus(status);
        collects.setStoreid(storeId);

        boolean save=collectsService.save(collects);
        System.out.println(save);
        return Result.ok();

    }
}
